package models

import (
	"log"
	"quantization_trading/src/util"
	"strings"
)

type Price struct {
	Id int64 `json:"id"`
	Exchange string `json:"exchange"` //交易所
	ContractType string `json:"contract_type"` //合约类型
	Symbol string `json:"symbol"`  //交易对
	NewPrice float64 `json:"new_price"` //最新价
}



func AddPrice(price *Price) int {
	var prices []Price
	db.Where("exchange = ? and symbol = ? and contract_type = ?",strings.ToLower(price.Exchange),strings.ToLower(price.Symbol),strings.ToLower(price.ContractType)).Find(&prices)
	if len(prices) == 0{
		if err := db.Create(price).Error; err != nil {
			log.Fatal(err)
			return util.ADD_ERROR
		}
		return util.ADD_SUSSESS
	}else {
		return util.ADD_HAVE_DATA
	}

}

func DelPrice(price *Price) int {
	db.Where("exchange=? and contract_type=? and symbol=?",price.Exchange,price.ContractType,price.Symbol).Delete(Price{})
	//db.Delete(price)
	return util.DELETE_SUSSESS_PRICE
}

func FindAllPrice() ([]Price, error) {
	var price_list []Price
	db.Find(&price_list)
	return price_list,nil
}